package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.PictureAdapter;
import com.sx.zongzhi.bean.BoxSearchBean;
import com.sx.zongzhi.bean.MailBean;
import com.sx.zongzhi.presenter.SendBoxDetailPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.ISendBoxDetailView;
import com.sx.zongzhi.voiceamr.Player;
import com.sx.zongzhi.widget.CustomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 接收消息详情
 */
public class SendBoxDetailActivity extends BaseActivity implements ISendBoxDetailView {

    @BindView(R.id.tv_Time)
    TextView tv_Time;
    @BindView(R.id.tv_Title)
    TextView tv_Title;
    @BindView(R.id.tv_Object)
    TextView tv_Object;
    @BindView(R.id.tv_Content)
    TextView tv_Content;

    @BindView(R.id.recyclerView)
    RecyclerView rv_imageRV;

    private String messageId;

    private Player player ;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_send_box_detail);
        setTitleName("发送消息详情");
        setRightImage(R.mipmap.icon_nav_delete);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        messageId = getIntent().getStringExtra("messageId");
        new SendBoxDetailPest(this).getSendBoxDetail(this,messageId);
    }

    @OnClick({R.id.imv_Right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Right:
                createDelete();
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除此消息吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                new SendBoxDetailPest(SendBoxDetailActivity.this).deleteSendBox(SendBoxDetailActivity.this,messageId);
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    public void getSendBoxDetailSuccess(MailBean mailBean) {
        if (mailBean != null){
            initData(mailBean);
        }
    }
    private void initData(final MailBean mailBean) {
        tv_Time.setText(mailBean.sendDate);
        tv_Title.setText(mailBean.title);
        tv_Content.setText(mailBean.content);
        tv_Object.setText(mailBean.receiverNames);
        if (mailBean.files != null && mailBean.files.size() > 0){
            rv_imageRV.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false));
            final PictureAdapter pictureAdapter = new PictureAdapter(SendBoxDetailActivity.this, mailBean.files);
            rv_imageRV.setAdapter(pictureAdapter);
//            pictureAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                @Override
//                public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
//                    String type = mailBean.files.get(position).getSuffixType();
//                    String path = ServiceFactory.NEW_API_BASE_URL+ File.separator + mailBean.files.get(position).getPath();
//                    if(".mp4".equalsIgnoreCase(type)){
////                        startActivity(new Intent(EventDescribeDetailActivity.this, VideoPlayerActivity.class).putExtra(
////                                "path",path));
//                    }else if(".wav".equalsIgnoreCase(type) || ".amr".equalsIgnoreCase(type)){
//                        int isPlayingPostion = 0;
//                        for(int i = 0 ; i < mailBean.files.size() ; i++){
//                            if (mailBean.files.get(i).isPlay){
//                                isPlayingPostion = i;
//                                break;
//                            }
//                        }
//                        if (player == null){
//                            player = new Player(SendBoxDetailActivity.this);
//                        }
//                        if (player.isPlay() && isPlayingPostion == position){
//                            player.stop();
//                            mailBean.files.get(isPlayingPostion).isPlay = false;
//                            pictureAdapter.notifyDataSetChanged();
//                        }else if (player.isPlay() && isPlayingPostion != position){
//                            player.stop();
//                            mailBean.files.get(isPlayingPostion).isPlay = false;
//                            mailBean.files.get(position).isPlay = true;
//                            pictureAdapter.notifyDataSetChanged();
//                            player.playUrl(path,pictureAdapter.tv_num, new Player.OnCompletionListener() {
//                                @Override
//                                public void onPlayComplet() {
//                                    mailBean.files.get(position).isPlay = false;
//                                    pictureAdapter.notifyDataSetChanged();
//                                }
//                            });
//                        }else {
//                            mailBean.files.get(position).isPlay = true;
//                            pictureAdapter.notifyDataSetChanged();
//                            player.playUrl(path,pictureAdapter.tv_num,new Player.OnCompletionListener() {
//                                @Override
//                                public void onPlayComplet() {
//                                    mailBean.files.get(position).isPlay = false;
//                                    pictureAdapter.notifyDataSetChanged();
//                                }
//                            });
//                        }
//                    }else {
//                        startActivity(new Intent(SendBoxDetailActivity.this, ImagePlayActivity.class).putExtra(
//                                "path", path));
//                    }
//                }
//            });
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void deleteSendBoxSuccess(Boolean deleteResult) {
        ToastUtils.showToast(this,"删除成功");
        BoxSearchBean boxSearchBean = new BoxSearchBean();
        boxSearchBean.mailType = 1;
        MyRxBus.get().post(boxSearchBean);
        finish();
    }

    @Override
    public void deletSendBoxFail(String message) {
        ToastUtils.showToast(this,message);
    }


}
